package com.leijie.nio;

import java.nio.ByteBuffer;

/**
 * slice buffer 的功能和用方法
 * 共享buffer
 */
public class NioTest6 {

    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);

        for (int i=0;i<buffer.capacity();++i){
            buffer.put((byte) i);
        }

        buffer.position(2);
        buffer.limit(6);

        ByteBuffer sliceBuffer = buffer.slice();

        for (int i=0;i<sliceBuffer.capacity();++i){
            byte a = sliceBuffer.get(i);
            a *=2;
            sliceBuffer.put(a);
        }
        //还原postion 和limit
        buffer.position(0);
        buffer.limit(buffer.capacity());
//        buffer.clear();
        //遍历原buffer 看看有没有变化
        for (int i=0;i<buffer.capacity();++i){
            System.out.println(buffer.get(i));
        }
    }
}
